package com.ms.qw.jsf;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.util.ResourceBundle;

/**
 * User: aglwkrs
 * Date: 25/11/11
 */
@ManagedBean
@Scope("session")
@Component
public class LoginJsfBean {
    private static final String resourceBundleName = "messages";
    private static String SHOW_LOCAL = "01"; //rol del usuario que exige habilitar el campo "Local"

    String username;
    String password;
    private String rol;
    private String version;


    @PostConstruct
    public void init() {
    }

    public String loginAction() throws IOException, ServletException {

        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()).getRequestDispatcher("/j_spring_security_check");
        dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse());
        FacesContext.getCurrentInstance().responseComplete();
        return null;

    }

    protected ResourceBundle getResourceBundle() {

        FacesContext facesContext = FacesContext.getCurrentInstance();
        ResourceBundle bundle = facesContext.getApplication().
                getResourceBundle(facesContext, resourceBundleName);

        return bundle;
    }



    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRol() {
        return rol;
    }

    public void setRol(String rol) {
        this.rol = rol;
    }



    public boolean getSHOW_LOCAL() {
        return SHOW_LOCAL.equalsIgnoreCase(rol);
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}
